<?php 
require_once 'BaseMySQLDAO.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../Entity/Domain/MemberDomain.php';
class MemberDAO extends BaseMySQLDAO
{
	public function __construct($context) {
		parent::__construct($context, MemberDomain::$CollectionName);
	}

	public function create($obj){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "INSERT INTO member(memberID,email,pass,firstName,lastName,address,floor,elevator,phone,mobile,deliveryAddress,billingAddress,preferredDeliveryDay,preferredDeliveryTime,zipcode,countryShortCode,isAdmin)
					VALUES	(
								".$obj->getMemberID().",
								'".$obj->getEmail()."',
								'".$obj->getPass()."',
								'".$obj->getFirstName()."',
								'".$obj->getLastName()."',
								'".$obj->getAddress()."',
								".$obj->getFloor().",
								".$obj->getElevator().",
								'".$obj->getPhone()."',
								'".$obj->getMobile()."',
								'".$obj->getDeliveryAddress()."',
								'".$obj->getBillingAddress()."',
								'".$obj->getPreferredDeliveryDay()."',
								".$obj->getPreferredDeliveryTime().",
								'".$obj->getZipcode()."',
								'".$obj->getCountryShortCode()."',
								".$obj->getIsAdmin()."
							)";
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function deleteByOid($oid){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "DELETE FROM member WHERE memberID=".$oid;
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function update($obj){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "UPDATE member
				SET
					member.email = '".$obj->getEmail()."',
					member.pass = '".$obj->getPass()."',
					member.firstName = '".$obj->getFirstName()."',
					member.lastName = '".$obj->getLastName()."',
					member.address = '".$obj->getAddress()."',
					member.floor = ".$obj->getFloor().",
					member.elevator = ".$obj->getElevator().",
					member.phone = '".$obj->getPhone()."',
					member.mobile = '".$obj->getMobile()."',
					member.deliveryAddress = '".$obj->getDeliveryAddress()."',
					member.billingAddress = '".$obj->getBillingAddress()."',
					member.preferredDeliveryDay = '".$obj->getPreferredDeliveryDay()."',
					member.preferredDeliveryTime = ".$obj->getPreferredDeliveryTime().",
					member.zipcode = '".$obj->getZipcode()."',
					member.countryShortCode = '".$obj->getCountryShortCode()."',
					member.isAdmin = ".$obj->getIsAdmin()."
				 WHERE memberID=".$obj->getMemberID();
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function findByOid($oid)
	{
		$memberDomain = null;
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT * FROM member WHERE member.memberID = ".$oid;
		$res = $this->getMySQLConnection()->mysqlOneRow($inSQL);
		if($res)
		{
			$memberDomain = new MemberDomain($res);
		}
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $memberDomain;
	}

	public function findAll()
	{
		$memberDomainArr = array();
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT * FROM member";
		$res = $this->getMySQLConnection()->mysqlData($inSQL);
		if($res)
		{
			while ($data = mysql_fetch_assoc($res))
			{
				$memberDomain = new MemberDomain($data);
				$memberDomainArr[] = $memberDomain;
			}
		}
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $memberDomain;
	}
	
	public function findByEmailPass($email, $password)
	{
		$memberDomain = null;
		$this->getContext()->getDBTransaction()->startTransaction();		
		$inSQL = "SELECT * FROM member WHERE member.email = '{$email}' AND member.pass = '{$password}'"; 
		$res = $this->getMySQLConnection()->mysqlOneRow($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
		if($res != false)
			$memberDomain = new MemberDomain($res);
		return $memberDomain;
	}

}
?>